package com.ewzj.bigdata.mvp.model;

import android.app.Application;

import com.ewzj.bigdata.app.constants.AppConstants;
import com.ewzj.bigdata.app.utils.Apputils;
import com.ewzj.bigdata.mvp.model.api.service.CommonService;
import com.ewzj.bigdata.mvp.model.entity.reqbeans.ReqPageBean;
import com.ewzj.bigdata.mvp.model.entity.resbean.BaseData;
import com.ewzj.bigdata.mvp.model.entity.resbean.BaseListData;
import com.ewzj.bigdata.mvp.model.entity.resbean.NewBean;
import com.ewzj.bigdata.mvp.model.entity.resbean.SearchResultBean;
import com.google.gson.Gson;
import com.jess.arms.integration.IRepositoryManager;
import com.jess.arms.mvp.BaseModel;

import com.jess.arms.di.scope.ActivityScope;

import javax.inject.Inject;

import com.ewzj.bigdata.mvp.contract.SearchResultContract;

import io.reactivex.Observable;


@ActivityScope
public class SearchResultModel extends BaseModel implements SearchResultContract.Model {
    private Gson mGson;
    private Application mApplication;
    private CommonService service;
    @Inject
    public SearchResultModel(IRepositoryManager repositoryManager, Gson gson, Application application) {
        super(repositoryManager);
        this.mGson = gson;
        this.mApplication = application;
        this.service=repositoryManager.obtainRetrofitService(CommonService.class);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mGson = null;
        this.mApplication = null;
        this.service=null;
    }

    @Override
    public Observable<BaseData<BaseListData<NewBean>>> getSearchList(int page,SearchResultBean searchResultBean) {
        ReqPageBean reqPageBean=new ReqPageBean(page, AppConstants.PAGE_NUM);
        ReqPageBean.QueryEntity queryEntity=new ReqPageBean.QueryEntity();
        queryEntity.setCategory_id(searchResultBean.getCategory_id());
        queryEntity.setKeywords(searchResultBean.getKeywords());
        reqPageBean.setQuery(queryEntity);
        return service.getSearchList(Apputils.json2Body(mGson.toJson(reqPageBean)));
    }
}